<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="leaveQuery.name" placeholder="用户名" />
      </el-form-item>

      <el-form-item>
        <el-date-picker
          v-model="leaveQuery.begin"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="leaveQuery.end"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>

    <!-- 表格 -->
    <el-table :data="list" border fit highlight-current-row>
      <el-table-column label="ID号  " width="70" align="center">
        <template slot-scope="scope">
          <!-- {{ (page - 1) * limit + scope.$index + 1 }} -->
          {{ scope.row.id }}
        </template>
      </el-table-column>

      <el-table-column prop="internName" label="企业名称" width="200"> </el-table-column>

      <el-table-column prop="stuName" label="学生名" width="200"> </el-table-column>

      <el-table-column prop="leaveStartTime" label="请假时间段" width="350">
        <template slot-scope="scope">
          {{ scope.row.leaveStartTime }} —— {{ scope.row.leaveEndTime }}
        </template>
      </el-table-column>
      <el-table-column prop="leaveContent" label="请假内容" width="300">
      </el-table-column>
      <el-table-column prop="leaveStatus" label="状态" width="100">
          <template slot-scope="scope">
              {{scope.row.leaveStatus?"通过":"未通过"}}
          </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="warning" size="mini" plain @click="updateLeave(scope.row.id)"
            >修改请假</el-button
          >
          <el-button
            type="success"
            size="mini"
            plain
            @click="deleteLeaveById(scope.row.id)"
            >删除请假</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="修改请假" :visible.sync="updateEvaluateVisible">
      <el-form :model="leaveObj">
        <el-form-item label="企业名称" :label-width="formLabelWidth">
          <el-input v-model="leaveObj.internName" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="学生名" :label-width="formLabelWidth">
          <el-input disabled v-model="leaveObj.stuName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="请假开始时间" :label-width="formLabelWidth">
          <el-date-picker
            v-model="leaveObj.leaveStartTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="请假结束时间" :label-width="formLabelWidth">
          <el-date-picker
            v-model="leaveObj.leaveEndTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="请假内容" :label-width="formLabelWidth">
          <el-input
            v-model="leaveObj.leaveContent"
            type="textarea"
            maxlength="200"
            show-word-limit
            rows="6"
          ></el-input>
        </el-form-item>
        <el-form-item label="请假状态" :label-width="formLabelWidth">
          <el-switch
            v-model="leaveObj.leaveStatus"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="updateEvaluateVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateLeaveById(leaveObj)">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />
  </div>
</template>

<script>
import leaveApi from "@/api/leave";
import { mapGetters } from "vuex";

export default {
  computed: {
    ...mapGetters(["name", "roles"]),
  },
  data() {
    return {
      list: null,
      page: 1, //当前页
      limit: 10, //每页记录数
      total: 0, //总记录数
      leaveQuery: {}, //条件封装对象
      //学生列表
      leaveObj: {
        id: "",
        sid: "",
        stuName: "",

        iid: "",
        leaveStartTime: "",
        leaveEndTime: "",
        leaveContent: "",
        leaveStatus: "",
      },
      updateEvaluateVisible: false,
      formLabelWidth: "120px",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //获取请假列表
    getList() {
      this.role = this.roles[0];
      if(this.role == '学校管理员' || this.role == '企业管理员') {
        //
        this.leaveQuery.associationName = this.name;
      }
      leaveApi.getLeaveList(this.page, this.limit, this.leaveQuery).then((response) => {
        this.list = response.data.userLeaves;
        this.total = response.data.total;
      });
    },

    //打开修改请假
    updateLeave(id) {
      //将id赋值都vue对象参数内
      this.LeaveId = id;
      this.updateEvaluateVisible = true;

      //获取当前选中的评价行记录
      leaveApi.getLeaveById(id).then((response) => {
        this.leaveObj = response.data.leave;
      });
    },

    //修改请假
    updateLeaveById(leave) {
      leaveApi.saveOrUpdateLeave(leave).then((resp) => {
        this.$message({
          type: "success",
          message: "更新成功!",
        });
        this.updateEvaluateVisible = false;
        this.getList();
      });
    },

    //删除
    deleteLeaveById(id) {
      this.$confirm("此操作将删除评价, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        leaveApi.deleteLeaveById(id).then((response) => {
          //提示信息
          this.$message({
            type: "success",
            message: "移除成功!",
          });
          //再次渲染
          this.getList();
        });
      });
    },
  },
};
</script>

<style></style>
